package com.seeyon.apps.ebs.rcyh.utils;

import com.alibaba.fastjson.JSONArray;
import com.seeyon.apps.ebs.rcyh.dao.RcyhDao;
import com.seeyon.apps.ebs.rcyh.po.EbsRcMxPo;
import com.seeyon.apps.ebs.rcyh.po.EbsRcPo;
import com.seeyon.ctp.common.AppContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class RcyhUtils {
    private static Log log = LogFactory.getLog(RcyhUtils.class);
    private static RcyhDao rcyhDao = (RcyhDao) AppContext.getBean("rcyhDao");
    private static SimpleDateFormat adf = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
    * 功能:公共调用
    * @Date: 2025/4/18
    */
    public static void rcMx(JSONArray param, int state,EbsRcPo ebsRcPo) throws Exception {
        try {
            for (int i = 0; i < param.size(); i++) {
                String dataStr = param.getString(i);
                LocalDate dateLocalDate = LocalDate.parse(dataStr);
                Date dqDate = Date.from(dateLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
                EbsRcMxPo ebsRcMxPo = new EbsRcMxPo();
                ebsRcMxPo.setzId(ebsRcPo.getId());
                ebsRcMxPo.setMemberId(ebsRcPo.getMemberId());
                ebsRcMxPo.setName(ebsRcPo.getName());
                ebsRcMxPo.setDepartmentId(ebsRcPo.getDepartmentId());
                ebsRcMxPo.setYear(ebsRcPo.getYear());
                ebsRcMxPo.setMonth(ebsRcPo.getMonth());
                ebsRcMxPo.setDqDate(dqDate);
                ebsRcMxPo.setState(state);
                EbsRcMxPo ebsRcMxPoNew = RcyhUtils.updateRcMx(ebsRcMxPo);
                log.info("zg");
            }
            log.info("");
        }catch (Exception e){
            log.error("公共调用失败", e);
        }
    }

    /**
     * 功能:更新/新增 EBS_RC_MX
     * @Date: 2025/4/17
     */
    public static EbsRcMxPo updateRcMx(EbsRcMxPo ebsRcMxPo) throws Exception {
        EbsRcMxPo mbEbsRcMxPo = null;
        try {
            //查询是否有该条记录
            Map<String,Object> dqMxMap = new HashMap<>();
            dqMxMap.put("zid",ebsRcMxPo.getzId());
            dqMxMap.put("dq_date",adf.format(ebsRcMxPo.getDqDate()));
            List<Map<String, Object>> rcmxs = rcyhDao.getRcMx(dqMxMap);
            //更新/新增 EBS_RC_MX
            if (rcmxs!=null && !rcmxs.isEmpty()){
                log.info("更新 EBS_RC_MX");
                Long id = Long.valueOf(rcmxs.get(0).get("id").toString());
                List<EbsRcMxPo> rcMx = rcyhDao.getRcMxById(id);
                if (rcMx!=null && !rcMx.isEmpty()){
                    EbsRcMxPo ebsRcMx = rcMx.get(0);
                    ebsRcMx.setState(ebsRcMxPo.getState());
                    rcyhDao.updateRcMx(ebsRcMx);
                    mbEbsRcMxPo = ebsRcMx;
                }
            }else{
                ebsRcMxPo.setNewId();
                rcyhDao.addRcMx(ebsRcMxPo);
                mbEbsRcMxPo = ebsRcMxPo;
            }
        }catch (Exception e) {
            log.error("更新/新增 EBS_RC_MX失败", e);
        }
        return mbEbsRcMxPo;
    }

    /**
    * 功能:更新/新增 EBS_RC
    * @Date: 2025/4/17
    */
    public static EbsRcPo updateRc(EbsRcPo ebsRPo) throws Exception {
        EbsRcPo mbEbsRcPo = null;
        try {
            //查询是否有该条记录
            List<EbsRcPo> rcs = rcyhDao.getRc(ebsRPo);
            //更新/新增 EBS_RC
            if (rcs!=null && !rcs.isEmpty()){
                EbsRcPo ebsRcPo = rcs.get(0);
                ebsRcPo.setXjDays(ebsRPo.getXjDays());
                ebsRcPo.setZgDays(ebsRPo.getZgDays());
                ebsRcPo.setCcDays(ebsRPo.getCcDays());
                ebsRcPo.setUpdateDate(ebsRPo.getUpdateDate());
                mbEbsRcPo = ebsRcPo;
                rcyhDao.updateRc(ebsRcPo);
                //更新 EBS_RC
            }else{
                ebsRPo.setNewId();
                rcyhDao.addRc(ebsRPo);
                mbEbsRcPo = ebsRPo;
                //新增 EBS_RC
            }
        }catch (Exception e) {
            log.error("更新/新增 EBS_RC失败", e);
        }
        return mbEbsRcPo;
    }

    /**
     * 计算两个年份之间的所有年份（可选是否包含起始和结束年份）
     * @param startYear 起始年份
     * @param endYear 结束年份
     * @param inclusive 是否包含起始和结束年份
     * @return 包含所有年份的列表
     * @throws IllegalArgumentException 如果起始年份大于结束年份
     */
    public static List<Integer> getYearsBetween(int startYear, int endYear, boolean inclusive) {
        List<Integer> years = new ArrayList<>();
        try {
            if (startYear > endYear) {
                log.error("起始年份不能大于结束年份");
            }
            int start = inclusive ? startYear : startYear + 1;
            int end = inclusive ? endYear : endYear - 1;

            for (int year = start; year <= end; year++) {
                years.add(year);
            }
        }catch (Exception e){
            log.error("计算两个年份之间的所有年份失败", e);
        }
        return years;
    }

}
